package com.shigu.admin.diy.services;

import com.opentae.data.mall.beans.DiyPage;
import com.opentae.data.mall.beans.DiySearchParam;
import com.opentae.data.mall.beans.DiySql;
import com.opentae.data.mall.examples.DiyPageExample;
import com.opentae.data.mall.examples.DiySearchParamExample;
import com.opentae.data.mall.examples.DiySqlExample;
import com.opentae.data.mall.interfaces.DiyPageMapper;
import com.opentae.data.mall.interfaces.DiySearchParamMapper;
import com.opentae.data.mall.interfaces.DiySqlMapper;
import com.shigu.admin.diy.bo.DiyPageBO;
import com.shigu.admin.diy.bo.PageQueryBo;
import com.shigu.admin.diy.vo.DiyPageVO;
import com.shigu.admin.diy.vo.SearchParamVO;
import com.shigu.admin.manager.common.ShiguPager;
import com.shigu.component.utils.BeanMapper;
import com.shigu.exceptions.BaohuoException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Objects;

/**
 * 页面服务
 * Created by whx on 2017/7/11 0011.
 * @author bugzy
 */
@Service
public class PageService {

    @Autowired
    private DiyPageMapper diyPageMapper;

    @Autowired
    private DiySqlMapper diySqlMapper;

    @Autowired
    private SqlService sqlService;

    @Autowired
    private DiySearchParamMapper diySearchParamMapper;

    public void delPage(Long pageId){
        diyPageMapper.deleteByPrimaryKey(pageId);
    }

    @Transactional(rollbackFor = Exception.class)
    public void addPage(DiyPageBO bo) throws BaohuoException {
        DiyPage diyPage = BeanMapper.map(bo,DiyPage.class);
        diyPage.setOuterUrl(bo.getPageUrl());
        if (diyPage.getPageId() != null) {
            diyPageMapper.updateByPrimaryKeySelective(diyPage);
        } else {
            if (diyPage.getTitle()==null){
                throw new BaohuoException("请填写标题");
            }
            if (diyPage.getFieldMap()==null && diyPage.getType() == 1){
                throw new BaohuoException("请填写列设定");
            }
            diyPageMapper.insertSelective(diyPage);
        }

        if (bo.getSqls()!=null) {
            DiySqlExample sqlExample = new DiySqlExample();
            sqlExample.createCriteria().andUseForEqualTo(1).andTargetIdEqualTo(diyPage.getPageId());
            diySqlMapper.deleteByExample(sqlExample);
            DiySql diySql;
            if (diyPage.getType() == 1) {
                List<String> sql = new ArrayList<>(1);
                sql.add(StringUtils.join(bo.getSqls(), ", "));
                bo.setSqls(sql);
            }
            for (String sql:bo.getSqls()){
                diySql = new DiySql();
                diySql.setSqlContent(sql);
                diySql.setUseFor(1);
                diySql.setTargetId(diyPage.getPageId());
                diySqlMapper.insertSelective(diySql);
            }
        }
        if (bo.getParams() != null) {
            DiySearchParamExample searchParamExample = new DiySearchParamExample();
            searchParamExample.createCriteria().andPageIdEqualTo(diyPage.getPageId());
            diySearchParamMapper.deleteByExample(searchParamExample);
            for (SearchParamVO paramVO : bo.getParams()) {
                DiySearchParam param = BeanMapper.map(paramVO, DiySearchParam.class);
                param.setPageId(diyPage.getPageId());
                diySearchParamMapper.insertSelective(param);
            }
        }
    }

    public DiyPageVO selDiyPageByIdAndType(Long pageId,Integer type) {
        DiyPage diyPage = diyPageMapper.selectByPrimaryKey(pageId);
        if (diyPage == null || !Objects.equals(diyPage.getType(), type)) {
            return null;
        }
        DiyPageVO diyPageVO = BeanMapper.map(diyPage,DiyPageVO.class);
        diyPageVO.setUrl(diyPage.getOuterUrl());
        diyPageVO.setSqls(sqlService.sqlsByUserfor(1,diyPageVO.getPageId()));
        diyPageVO.setFieldMapper(new LinkedHashMap<String, String>());
        if (diyPageVO.getFieldMap() != null) {
            for (String s : diyPageVO.getFieldMap().split(",")) {
                String[] split = s.split(":");
                if (split.length == 2) {
                    diyPageVO.getFieldMapper().put(split[0], split[1]);
                }
            }
        }
        if (diyPageVO.getFieldMapper().isEmpty()) {
            diyPageVO.setFieldMapper(null);
        }
        DiySearchParamExample paramExample = new DiySearchParamExample();
        paramExample.setOrderByClause("param_id asc");
        paramExample.createCriteria().andPageIdEqualTo(diyPage.getPageId());
        diyPageVO.setSearchParams(BeanMapper.mapList(diySearchParamMapper.selectByExample(paramExample), SearchParamVO.class));
        return diyPageVO;
    }

    public ShiguPager<DiyPageVO> queryPages(PageQueryBo bo) {
        ShiguPager<DiyPageVO> pager = new ShiguPager<>();
        pager.setNumber(bo.getCurrentPage());
        DiyPageExample pageExample = new DiyPageExample();
        pageExample.setStartIndex((bo.getCurrentPage() - 1) * bo.getPageSize());
        pageExample.setEndIndex(bo.getPageSize());
        pageExample.setOrderByClause("page_id desc");
        DiyPageExample.Criteria criteria = pageExample.createCriteria();

        if (bo.getPageId() != null) {
            criteria.andPageIdEqualTo(bo.getPageId());
        }
        if (bo.getType() != null) {
            criteria.andTypeEqualTo(bo.getType());
        }

        int count = diyPageMapper.countByExample(pageExample);
        if (count > 0) {
            pager.calPages(count, bo.getPageSize());
            pager.setContent(BeanMapper.mapList(diyPageMapper.selectByConditionList(pageExample), DiyPageVO.class));
        }
        return pager;
    }

}
